#include <windows.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "resource.h"
#define IDR_MENU1 101;
int max;
const char g_szClassName[] = "Algoritmul Rabbin Karp";
char AppCaption[40],buffer[1024];
int textbox_id = 156;
char SIR[1024],SABLON[1024];
HWND text;
int k,k1; int key=29,c=0,contor=0;
char s[1000],s2[1000]/*,sir[1024]*/,s3[1000],s4[2];

int functiehash(char s[],int key){
	int b=0,a=1;
	for(int i=0;i<strlen(s);i++){
		for(int j=0;j<strlen(s)-i-1;j++)
			a*=key;
		b+=a*s[i];
		a=1;
	}
	return b;


}

BOOL CALLBACK DIALOG1(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	int lungime=GetWindowTextLength(GetDlgItem(hwnd,IDC_EDIT2));
	int lungime2=GetWindowTextLength(GetDlgItem(hwnd,IDC_EDIT1));
	 switch(msg)
    {
	 
        case WM_INITDIALOG:
			return TRUE;
			break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{

				case IDC_BUTTON2:
				
					GetDlgItemText(hwnd,IDC_EDIT2,SABLON,lungime+1);
					SetDlgItemText(hwnd,IDC_EDIT5,SABLON);
				
					break;
				case  IDC_reset1:
					SetDlgItemText(hwnd,IDC_EDIT1,"");
				break;
				case  IDC_reset2:
					SetDlgItemText(hwnd,IDC_EDIT1,"");
					SetDlgItemText(hwnd,IDC_EDIT2,"");
					SetDlgItemText(hwnd,IDC_EDIT4,"");
					SetDlgItemText(hwnd,IDC_EDIT5,"");
					SetDlgItemText(hwnd,IDC_EDIT6,"");
					SetDlgItemText(hwnd,IDC_EDIT7,"");
					break;
				case IDC_OK1:
				GetDlgItemText(hwnd,IDC_EDIT1,SIR,lungime2+1);
				break;

				case IDC_skip:
				{ int v[100],j=0;
					k=functiehash(SABLON,key);
					//sprintf(s3,"%d",k);
					//SetDlgItemText(hwnd,IDC_EDIT4,s3);

					for(int i=0;i<strlen(SIR)-strlen(SABLON)+1;i++)
					{
					strncpy(s2,SIR+i,strlen(SABLON));
					strcpy(s2+strlen(SABLON),"");
					//sprintf(s3,"%s",s2);
					//SetDlgItemText(hwnd,IDC_EDIT7,s2);

					k1=functiehash(s2,key);
					//sprintf(s2,"%d",k1);
					//SetDlgItemText(hwnd,IDC_EDIT6,s2);
				if(k==k1)
				{ v[j++]=i;
					c++;
				}
				
					}
					
					sprintf(s,"Sablonul a fost gasit de %d ori in text\n ",c);
					for(int t=0;t<j;t++){
						strcat(s,"pozitia: ");
						itoa(v[t]+1,s2,10);
						strcat(s,s2);
						strcat(s,"\n");
					}

					MessageBox(hwnd,s,"",MB_OK);
					break;
				}


				case IDC_START3:{
					
					contor=0;
					k=functiehash(SABLON,key);
					sprintf(s3,"%d",k);
					SetDlgItemText(hwnd,IDC_EDIT4,s3);

					strncpy(s2,SIR+contor,strlen(SABLON));
					strcpy(s2+strlen(SABLON),"");
					sprintf(s3,"%s",s2);
					SetDlgItemText(hwnd,IDC_EDIT7,s2);


					strncpy(s4,SIR+strlen(SABLON),1);
					sprintf(s3,"<%s>",s4);
					SetDlgItemText(hwnd,IDC_EDIT8,s3);

					if(strcmp(s2,"")==0)
					{
						MessageBox(hwnd,"Nu ati introdus niciun text","ERROR",MB_OK);
						break;
					}
					k1=functiehash(s2,key);
					sprintf(s3,"%d",k1);
					SetDlgItemText(hwnd,IDC_EDIT6,s3);

					if(k==k1){
						MessageBox(hwnd,"Sablonul a fost gsit","",MB_OK);
					};
					
					break;
								}
				case IDC_next:{
					contor++;
					if (contor>=strlen(SIR)-strlen(SABLON)+1) {
						MessageBox(hwnd,"Textul a fost parcurs in intregime","",MB_OK);
						break;
					}
					k=functiehash(SABLON,key);
					sprintf(s3,"%d",k);
					SetDlgItemText(hwnd,IDC_EDIT4,s3);

					strncpy(s2,SIR+contor,strlen(SABLON));
					strcpy(s2+strlen(SABLON),"");
					sprintf(s3,"%s",s2);
					SetDlgItemText(hwnd,IDC_EDIT7,s2);

					strncpy(s4,SIR+contor+strlen(SABLON),1);
					sprintf(s3,"<%s>",s4);
					SetDlgItemText(hwnd,IDC_EDIT8,s3);

					strncpy(s4,SIR+contor-1,1);
					sprintf(s3,"<%s>",s4);
					SetDlgItemText(hwnd,IDC_EDIT3,s3);

					k1=functiehash(s2,key);
					sprintf(s2,"%d",k1);
					SetDlgItemText(hwnd,IDC_EDIT6,s2);

					if(k==k1){
						MessageBox(hwnd,"Sablonul a fost gasit","",MB_OK);
					};
					break;
				}

				case WM_DESTROY:
				 PostQuitMessage(0); break;

					break;
			}
			

		default:return FALSE;
	 }
	 return TRUE;
}

// Step 4: the Window Procedure
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
	{
	HWND hwnd;
	max=1024;
	switch(Msg)
	{
	case WM_CLOSE:
           DestroyWindow(hWndDlg);
		break;
	case WM_INITDIALOG:
		return TRUE;break;

	case WM_COMMAND:
		switch(wParam)
		{
		case IDC_OK1:
			
			GetDlgItemText(text,IDC_EDIT1,SIR,max);
			
			
			break;
		/*case WM_DESTROY:
            PostQuitMessage(0);
			break;*/
		}
		break;
	return FALSE;
	}
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	FILE *f;HDC hDC;
	COLORREF clrRedish = RGB(255, 25, 2);
	COLORREF clrBlue = RGB(12, 25, 255);
	HWND d1=NULL,d2=NULL;
	PAINTSTRUCT Ps;
	COLORREF clrNavy = RGB(0, 0, 128);
	COLORREF clrAqua = RGB(0, 255, 255);
    switch(msg)
    {
		case WM_CLOSE:
            DestroyWindow(hwnd);
			break;
		case WM_PAINT:
			hDC = BeginPaint(hwnd, &Ps);
			SetTextColor(hDC, clrBlue);
			TextOut(hDC, 50, 400, "Proiect realizat de:Ciobotaru Costel", 36);
			TextOut(hDC, 172, 415, "Cretu Cezar", 11);
			TextOut(hDC, 50, 430, "Grupa:", 6);
			TextOut(hDC, 100, 430, "E211A", 5);
			TextOut(hDC, 350, 400, "Indrumator:", 11);
			TextOut(hDC, 430, 400, "Mihai Pura", 10);
			EndPaint(hwnd, &Ps);
			break;
        case WM_CREATE:
		{
           CreateWindow(TEXT("button"), TEXT("&Incepeti Aplicatia"),    
		             WS_VISIBLE | WS_CHILD ,
		             20, 50, 150, 50,        
		             hwnd, (HMENU) 1, NULL, NULL);    

			 CreateWindow(TEXT("button"), TEXT("&Help"),    
		             WS_VISIBLE | WS_CHILD ,
		             20, 150, 150, 50,        
		             hwnd, (HMENU) 2, NULL, NULL);    
			  CreateWindow(TEXT("button"), TEXT("&Quit"),    
		             WS_VISIBLE | WS_CHILD ,
		             20, 250, 150, 50,        
		             hwnd, (HMENU) 3, NULL, NULL);    
	    break;
		}
		case WM_COMMAND:
			switch(LOWORD(	wParam))
			{
				case 1:
				d1=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DLGFIRST),hwnd,DIALOG1);
				{
					if (d1!=NULL)
					ShowWindow(d1,SW_SHOW);
					
				}
				break;
				case 2:
					MessageBox(hwnd,
						"Dupa ce ati introdus textul in oricare din ferestre(cea pentru sablon sau cea pentru text),apasati butonul"
						"<ok> pentru a putea procesa textul.Apoi apasati butonul <Start>"
						"Dupa ce ati apasat butonul <Start> fie apasati butonul <Next> pentru a face cautarea pas cu pas"
						"fie apasati <Cautare rapida> pentru a trece peste toti acesti pasi "
						"si a afisa de cate ori a fost gasit sablonu in text si pe ce pozitii a fost gasit"
						,"HELP",MB_OK);
				break;
				case 3:
					DestroyWindow(hwnd);
				break;
			}
			
		break;
        case WM_DESTROY:
            PostQuitMessage(0); break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{	
	PAINTSTRUCT Ps;
	COLORREF clrAqua = RGB(0, 255, 255);
    WNDCLASSEX wc;
	HWND text;
    HWND hwnd;
    MSG Msg;
	LoadString(hInstance, IDR_MAINFRAME, AppCaption, 40);
    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 10;
	wc.hIcon		 =LoadIcon(NULL,IDI_APPLICATION);//iconita la aplicatie
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);

    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//1,7-ALB;2-GRI,3,4-NEGRU,9,25-ALBASTRU DESCHIS,8,6-GRI
    wc.lpszMenuName  = NULL;//MAKEINTRESOURCE(IDR_MAINFRAME);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Algoritmul Rabbin-karp",//Numele icoanei
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT , CW_USEDEFAULT, 600, 600,
        NULL, NULL, hInstance, NULL);
    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
	

    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);
	

    // Step 3: The Message Loop
	
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
	
	
}


//---------------------------------------------------------------------------